package com.clp.protocol.iec104.iec104.server.slave;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Getter
@Slf4j
public class InSlaveInitializer extends ChannelInitializer<ServerSocketChannel> {
    private final InSlave inSlave;

    public InSlaveInitializer(InSlave inSlave, SlaveConfig cfg) {
        this.inSlave = inSlave;
    }

    @Override
    protected void initChannel(ServerSocketChannel ch) throws Exception {
        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                if (msg instanceof SocketChannel) {
                    SocketChannel socketChannel = (SocketChannel) msg;
                    // 读到新的socket连接
                    log.info("[{}] new connection arrived: {}", inSlave, socketChannel.remoteAddress());
                }
                ctx.fireChannelRead(msg);
            }
        });
    }


}
